package com.icesoft.icefaces.security;

import java.util.HashSet;
import java.util.Set;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    public AppUser findUser(String userName) {
    	System.out.println("Registered the driver ok, so let's make a connection.");
    	  
    	  Connection c = null;
    	  
    	  try {
    	    // The second and third arguments are the username and password,
    	    // respectively. They should be whatever is necessary to connect
    	    // to the database.
    	    c = DriverManager.getConnection("jdbc:postgresql://localhost/postgres",
    	                                    "postgres", "*plmnko0");
    	  } catch (SQLException se) {
    	    System.out.println("Couldn't connect: print out a stack trace and exit.");
    	    se.printStackTrace();
    	    System.exit(1);
    	  }
    	  
    	  if (c != null)
    	    System.out.println("Hooray! We connected to the database!");
    	  else
    	    System.out.println("We should never get here.");
  
    	
    	    	
        AppUser appUser = null;
        Set<String> roles = new HashSet<String>();
        if (userName.equals("john")) {
            roles.add("ROLE_URLACCESS");
            appUser = new AppUser("john", "John", "Turner", "john", roles);
        } else if (userName.equals("jim")) {
            appUser = new AppUser("jim", "Jim", "Daniel", "jim", roles);
        } else if (userName.equals("tina")) {
            roles.add("ROLE_ALLACCESS");
            appUser = new AppUser("tina", "Tina", "Joseph", "tina", roles);
        }
        return appUser;
    }   
    
}
